﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Net;
using ChrizNet.SearchService.Utils;

namespace ChrizNet.SearchService
{
    public class GoogleWebSearch
    {
        private Uri SearchEngineUrl = new Uri("http://www.google.com/search");

        private ulong GetResultsCount(string searchResults)
        {
            //About 645,000,000 results
            //Get the search results count by 'screen scraping' the HTML document
            string resultsCountMessage = "<div id=resultStats>About ";
            int resultsCountMessageLength = resultsCountMessage.Length;
            ulong resultsCount = 0;
            int pos = searchResults.IndexOf(resultsCountMessage);
            if (pos != -1)
            {
                int afterCount = searchResults.IndexOf(" results<nobr>");
                if (afterCount == -1)
                    afterCount = searchResults.IndexOf("</b> for");
                ulong.TryParse(searchResults.Substring(pos + resultsCountMessageLength, afterCount - (pos + resultsCountMessageLength)).Replace(",", ""), out resultsCount);
            }
            return resultsCount;
        }

        private ulong GetGoogleScholarResultsCount(string searchResults)
        {
            ulong resultsCount = 0;
            int pos = searchResults.IndexOf("of about <b>");
            if (pos != -1)
            {
                ulong.TryParse(searchResults.Substring(pos + 12, searchResults.IndexOf("</b>.   (<b>") - (pos + 12)).Replace(",", ""), out resultsCount);
            }
            return resultsCount;
        }

        public ulong GetDomainSize(string domainUrl)
        {
            var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy, Encoding = System.Text.Encoding.UTF8 };
            webClient.QueryString.Add("q", string.Format("site:{0}", domainUrl));

            //Download the search results page
            webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
            string searchResults = webClient.DownloadString(SearchEngineUrl);
            return GetResultsCount(searchResults);
        }

        public ulong GetInlinksCount(string domainUrl)
        {
            var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy, Encoding = System.Text.Encoding.UTF8 };
            webClient.QueryString.Add("q", string.Format("\"http://*.{0}\"", domainUrl));
            webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
            string searchResults = webClient.DownloadString(SearchEngineUrl);
            return GetResultsCount(searchResults);
        }

        public ulong GetRichDocumentsCount(string domainUrl, string fileType)
        {
            var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy, Encoding = System.Text.Encoding.UTF8 };
            webClient.QueryString.Add("q", string.Format("site:{0} filetype:{1}", domainUrl, fileType));
            webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
            string searchResults = webClient.DownloadString(SearchEngineUrl);
            return GetResultsCount(searchResults);
        }

        public ulong GetScholarCount(string domainUrl)
        {
            Uri googleScholarUrl = new Uri("http://scholar.google.com/scholar");
            var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy, Encoding = System.Text.Encoding.UTF8 };
            webClient.QueryString.Add("q", string.Format("site:{0}", domainUrl));
            webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
            string searchResults = webClient.DownloadString(googleScholarUrl);
            //Download the search results page
            ulong resultsCount = GetGoogleScholarResultsCount(searchResults);
            return resultsCount;
        }

        public List<String> GetResultsList(string target)
        {
            var retval = new List<String>();
            var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy, Encoding = System.Text.Encoding.UTF8 };
            webClient.QueryString.Add("q", target);
            webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
            string searchResults = webClient.DownloadString(SearchEngineUrl);
            ParseHTML parser = new ParseHTML();

            // Give the HTML file to read
            parser.Source = searchResults;

            // The ParseHTML class is an SAX parser. This means it will go
            // deeper in the document as you call ParseHTML.Parse()
            while (!parser.Eof())
            {
                // ParseHTML.Parse() returns the current character that's been
                // parsed. It returns 0 if the parsed character is an HTML tag.
                if (parser.Parse() == 0)
                {
                    Tag tag = parser.GetTag();

                    if (tag.Name == "a")
                    {
                        // We found a link! Google will add as class name "l"
                        // for search results, as a result if a link's class
                        // name is "l" it should be a search result.
                        if (tag.Attributes["class"] != null && tag.Attributes["class"].Value == "fl")
                        {
                            // This is a search result

                            // ParseHTML.GetTag gets the content of the current
                            // HTML tag
                            String linkContent = tag.Attributes["href"].Value;
                            retval.Add(linkContent);
                        }
                    }
                }
            }
            return retval;
        }

        public String GetResults(string target)
        {
            return String.Empty;
            //var webClient = new WebClient { Proxy = WebRequest.DefaultWebProxy, Encoding = System.Text.Encoding.UTF8 };
            //webClient.QueryString.Add("q", target);
            //webClient.Proxy.Credentials = CredentialCache.DefaultCredentials;
            //string searchResults = webClient.DownloadString(SearchEngineUrl);
            //return searchResults;
        }
    
    }
}
